<?php
/**
 * 生成唯一用户身份识别码
 * @return string
 */
function createSid()
{
    return md5(md5(uniqid() . rand(10000, 99999)) . md5(time()));
}
/**
 * 用户密码加密
 * @param $password
 * @return string
 */
function getPwd($pwd)
{
    return md5(md5($pwd) . md5('13242463'));
}
/**
 * 快捷生成sweetalert提示框
 *
 * @param      $_title
 * @param      $_text
 * @param      $_type
 * @param null $_url
 *
 * @return string
 */
function sweetAlert($_title, $_text, $_type, $_url = null)
{
    if (empty($_url)) {
        return 'swal("' . $_title . '", "' . $_text . '", "' . $_type . '");';
    } else {
        if ($_url == 'REFERER') {
            $_url = '/';
            if (isset($_SERVER['HTTP_REFERER'])) {
                $_url = $_SERVER['HTTP_REFERER'];
            }
        }
        if($_type==1){
            $_type = 'success';
        }elseif($_type==2){
            $_type = 'error';
        }elseif($_type==3){
            $_type = 'warning';
        }
        return 'swal({ title: "' . $_title . '",   text: "' . $_text . '",   type: "' . $_type . '",   showCancelButton: false,   confirmButtonColor: "#DD6B55",   confirmButtonText: "OK",   closeOnConfirm: false }, function(){   window.location.href="' . $_url . '"; });';
    }
}

/**
 * @param $email
 * @param $subject
 * @param $body
 * @param array $config
 * @return PHPMailer
 * @return 邮箱
 */
function sendEmail($email, $subject, $body, $config = [])
{
    if (empty($config)) $config = [
        'host' => config('web_email_host'),
        'port' => config('web_email_port'),
        'charset'  => config('web_email_charset'),
        'address'  => config('web_email_address'),
        'security' => config('web_email_security'),
        'username' => config('web_email_username'),
        'password' => config('web_email_password')
    ];
    require_once EXTEND_PATH . 'PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->CharSet = $config['charset'];
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = $config['host'];  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = $config['username'];                 // SMTP username
    $mail->Password = $config['password'];                           // SMTP password
    $mail->SMTPSecure = $config['security'];                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = $config['port'];                                    // TCP port to connect to
    $mail->setLanguage('ch', EXTEND_PATH . '/PHPMailer/language/');

    $mail->setFrom($config['address'], config('web_name'));
    $mail->addAddress($email);

    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = $subject;
    $mail->Body = $body;

    return $mail;
}

/**
 * @param $url
 * @return string
 * @链接转跳
 */
function getUrl($url)
{
    return 'window.location.href="' . $url . '";';
}